package com.usian.wemedia.controller;

import com.usian.apis.wemedia.WmNewsControllerApi;
import com.usian.model.admin.dtos.NewsAuthDto;
import com.usian.model.common.dtos.PageResponseResult;
import com.usian.model.common.dtos.ResponseResult;
import com.usian.model.common.enums.AppHttpCodeEnum;
import com.usian.model.media.dtos.WmNewsDto;
import com.usian.model.media.dtos.WmNewsPageReqDto;
import com.usian.model.media.pojos.WmNewVo;
import com.usian.model.media.pojos.WmNews;
import com.usian.wemedia.service.WmNewsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/v1/news")
public class WmNewController implements WmNewsControllerApi {

    @Autowired
    private WmNewsService wmNewsService;

    /**
     * 文章列表查询
     *
     * @param dto
     * @return
     */
    @PostMapping("/list")
    @Override
    public ResponseResult findList(@RequestBody WmNewsPageReqDto dto) {
        return wmNewsService.findList(dto);
    }


    /**
     * 文章保存、修改、草稿
     * @param wmNews
     * @return
     */
    @PostMapping("/submit")
    @Override
    public ResponseResult submitNews(@RequestBody WmNewsDto wmNews) {
        if(wmNews.getStatus().equals(WmNews.Status.SUBMIT.getCode())){
            return wmNewsService.saveNews(wmNews,(short) 1);
        }else {
            return wmNewsService.saveNews(wmNews,(short) 0);
        }
    }

    /**
     * 根据文章id查询回显
     *
     * @param newId
     * @return
     */
    @GetMapping("/one/{newId}")
    @Override
    public ResponseResult findWmNewsById(@PathVariable Integer newId) {
        return wmNewsService.findWmNewsById(newId);
    }

    /**
     * admin端审核 根据文章id查询文章详情
     *
     * @param id
     * @return
     */
    @Override
    @GetMapping("/findOne/{id}")
    public WmNews findById(@PathVariable Integer id) {
        return wmNewsService.getById(id);
    }

    /**
     * admin端审核 保存修改文章状态
     * @param wmNews
     * @return
     */

    @Override
    @PostMapping("/update")
    public ResponseResult updateWmNews(@RequestBody WmNews wmNews) {
        boolean result = wmNewsService.updateById(wmNews);
        if(result){
            return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
        }
        return ResponseResult.okResult(AppHttpCodeEnum.SERVER_ERROR);
    }

    /**
     * 定时任务发布文章 查询审核通过待发布的文章列表
     *
     * @return
     */
    @Override
    @GetMapping("/findRelease")
    public List<Integer> findRelease() {
        return wmNewsService.findRelease();
    }

    /**
     * 人工审核列表查询
     *
     * @param dto
     * @return
     */
    @Override
    @PostMapping("/findList")
    public PageResponseResult findNewsList(@RequestBody NewsAuthDto dto) {
        return wmNewsService.findNewsList(dto);
    }

    /**
     * 人工审核查看文章详情
     *
     * @param id
     * @return
     */
    @Override
    @GetMapping("/find_news_vo/{id}")
    public WmNewVo findOne(@PathVariable  Integer id) {
        return wmNewsService.findOne(id);
    }
}
